import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:schedule_self_client/api/schedule_self_server/task.dart';
import 'package:schedule_self_client/utils/device_size_util.dart';

class TaskSelectDialog extends StatefulWidget {
  final List<Task> tasks;

  const TaskSelectDialog({super.key, required this.tasks});

  @override
  State<TaskSelectDialog> createState() => _TaskSelectDialogState();
}

class _TaskSelectDialogState extends State<TaskSelectDialog> {
  Task? _focusTask;

  @override
  Widget build(BuildContext context) {
    double dialogWidth = DeviceSizeUtil.getScreenWidth(context) * 0.8;

    return AlertDialog(
      title: const Text('请选择专注的任务'),
      content: SizedBox(
        width: dialogWidth,
        child: ListView.builder(
          itemCount: widget.tasks.length,
          itemBuilder: (context, index) {
            return _buildTaskCard(widget.tasks[index], context);
          },
        ),
      ),
      actions: [
        TextButton(
          style: TextButton.styleFrom(
            textStyle: Theme.of(context).textTheme.labelLarge,
          ),
          child: const Text('取消'),
          onPressed: () {
            Navigator.of(context).pop(null);
          },
        ),
        TextButton(
          style: TextButton.styleFrom(
            textStyle: Theme.of(context).textTheme.labelLarge,
          ),
          child: const Text('确定'),
          onPressed: () {
            Navigator.of(context).pop(_focusTask);
          },
        ),
      ],
    );
  }

  Widget _buildTaskCard(Task task, BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState(() {
          _focusTask = task;
        });
      },
      child: Card(
        color: _focusTask?.id == task.id
            ? Theme.of(context).colorScheme.primaryContainer
            : null,
        child: Row(
          children: [
            _focusTask?.id == task.id
                ? const Icon(Icons.check_circle_outline)
                : const Icon(Icons.circle_outlined),
            const SizedBox(width: 8),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const SizedBox(
                    height: 8,
                  ),
                  Text(
                    task.name,
                    style: Theme.of(context).textTheme.labelLarge,
                  ),
                  const SizedBox(
                    height: 4,
                  ),
                  Row(
                    children: [
                      const Icon(Icons.calendar_today, size: 16),
                      const SizedBox(
                        width: 4,
                      ),
                      Text(task.deadline != null
                          ? _showCompleteTime(task.deadline!)
                          : '未设置截止日期'),
                    ],
                  ),
                  const SizedBox(
                    height: 8,
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  String _showCompleteTime(String completeTimeStr) {
    DateTime completeTime = DateTime.parse(completeTimeStr);
    DateFormat dateFormat =
        DateFormat('M月d日 E', Localizations.localeOf(context).languageCode);
    return dateFormat.format(completeTime);
  }
}
